package com.personal.book.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class LoginFilter
 */
//@WebFilter("/*")
public class LoginFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public LoginFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(true);
		String adminName = (String) session.getAttribute("adminName");
		String userName = (String) session.getAttribute("userName");
		if (req.getRequestURI().endsWith("adminLogin.jsp") || req.getRequestURI().endsWith("AdminServlet")
				|| req.getRequestURI().endsWith("index.jsp") || req.getRequestURI().endsWith("UserServlet")
				|| req.getRequestURI().endsWith("login.jsp") || req.getRequestURI().endsWith(".css")
				|| req.getRequestURI().endsWith(".js") || req.getRequestURI().endsWith(".png")
				|| req.getRequestURI().endsWith(".jpg") || req.getRequestURI().endsWith(".less")
				|| req.getRequestURI().endsWith(".scss") || req.getRequestURI().endsWith(".woff")
				|| req.getRequestURI().endsWith(".woff2") || req.getRequestURI().endsWith(".eot")
				|| req.getRequestURI().endsWith(".svg") || req.getRequestURI().endsWith(".ttf")
				|| req.getRequestURI().endsWith(".otf")) {
			chain.doFilter(request, response);
		} else if (adminName != null || userName != null) {
			chain.doFilter(request, response);
		} else if ((adminName == null || "".equals(adminName)) && (req.getRequestURI().endsWith("adminindex.jsp")
				|| req.getRequestURI().endsWith("addBookInfo.jsp")
				|| req.getRequestURI().endsWith("addBookTypeInfo.jsp") || req.getRequestURI().endsWith("addUsers.jsp")
				|| req.getRequestURI().endsWith("adminInfo.jsp") || req.getRequestURI().endsWith("adminOrders.jsp")
				|| req.getRequestURI().endsWith("adminOrdersDetails.jsp")
				|| req.getRequestURI().endsWith("adminindex.jsp") || req.getRequestURI().endsWith("bookInfo.jsp")
				|| req.getRequestURI().endsWith("bookTypeInfoUpdate.jsp")
				|| req.getRequestURI().endsWith("adminOrders.jsp")
				|| req.getRequestURI().endsWith("adminOrdersDetails.jsp")
				|| req.getRequestURI().endsWith("updateBookInfo.jsp") || req.getRequestURI().endsWith("userInfo.jsp")
				|| req.getRequestURI().endsWith("userUpdate.jsp")|| req.getRequestURI().endsWith("bookTypeInfo.jsp") )) {
			res.sendRedirect("adminLogin.jsp");
		} else if (userName == null || "".equals(userName)) {
			res.sendRedirect("login.jsp");
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
